package com.wx.business.weixin.protocol;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import com.eachonline.util.RandomStringGenerator;
import com.wx.business.weixin.common.Configure;
import com.wx.business.weixin.common.Signature;

public class AppPayReqData {

	private String appid;
	private String partnerid;
	private String prepayid;
	private String package1="Sign=WXPay";
	private String noncestr;
	private String timestamp;
	private String sign;

	public AppPayReqData(String prepayid) {
		// 微信分配的公众号ID（开通公众号之后可以获取到）
		setAppid(Configure.getAppid());

		// 微信支付分配的商户号ID（开通公众号的微信支付功能之后可以获取到）
		setPartnerid(Configure.getMchid());
		// 随机字符串，不长于32 位
		setNoncestr(RandomStringGenerator.getRandomStringByLength(32));
		setPrepayid(prepayid);
		setTimestamp(String.valueOf(System.currentTimeMillis() / 1000));

		// 根据API给的签名规则进行签名
		String sign = Signature.getSign(toMap());
		setSign(sign);
	}

	public String getAppid() {
		return appid;
	}

	public void setAppid(String appid) {
		this.appid = appid;
	}

	public String getPartnerid() {
		return partnerid;
	}

	public void setPartnerid(String partnerid) {
		this.partnerid = partnerid;
	}

	public String getPrepayid() {
		return prepayid;
	}

	public void setPrepayid(String prepayid) {
		this.prepayid = prepayid;
	}

	public String getPackage1() {
		return package1;
	}

	public void setPackage1(String package1) {
		this.package1 = package1;
	}

	public String getNoncestr() {
		return noncestr;
	}

	public void setNoncestr(String noncestr) {
		this.noncestr = noncestr;
	}

	public String getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(String timestamp) {
		this.timestamp = timestamp;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public Map<String, Object> toMap() {
		Map<String, Object> map = new HashMap<String, Object>();
		Field[] fields = this.getClass().getDeclaredFields();
		for (Field field : fields) {
			Object obj;
			try {
				obj = field.get(this);
				if (obj != null) {
					map.put(field.getName(), obj);
				}
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return map;
	}
}
